package com.zy.explore.license.application.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.explore.license.domain.mybatis.dao.SysEventLogDao;
import com.zy.explore.license.domain.mybatis.dao.UserMsgDao;
import com.zy.explore.license.domain.mybatis.entity.UserMsg;
import com.zy.explore.license.infrastructure.util.DateUtil;
import com.zy.explore.license.infrastructure.util.JwtUtil;
import com.zy.explore.license.interfaces.vo.DataOverViewVO;
import com.zy.explore.license.interfaces.vo.DayPvUvVO;
import com.zy.explore.license.interfaces.vo.RecentVisitDataVO;
import com.zy.explore.license.interfaces.vo.VisitItemVO;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * description 数据统计服务实现类
 *
 * @author ZY
 * @date 2021年05月27日 23:18
 */
@Slf4j
@Service
public class StatisticsService {

    @Resource
    private SysEventLogDao sysEventLogDao;

    @Resource
    private UserMsgDao userMsgDao;

    /**
     * 查询统计数据
     *
     * @return {@link DataOverViewVO}
     */
    public DataOverViewVO getDataOverView() {
        String beginDate = "2021-01-01 00:00:00";
        String endDate = DateUtil.format(new Date(), "yyyy-MM-dd") + " 23:59:59";
        int visitNum = this.sysEventLogDao.getVisitNum(beginDate, endDate);
        int visitorNum = this.sysEventLogDao.getVisitorNum(beginDate, endDate);
        QueryWrapper<UserMsg> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", JwtUtil.getCurrentUserId());
        long msgNum = userMsgDao.selectCount(wrapper);

        return DataOverViewVO.builder()
                .visitNum(visitNum)
                .visitorNum(visitorNum)
                .msgNum(Integer.parseInt(msgNum + ""))
                .build();
    }

    /**
     * 查询首页最近N天访问数据
     *
     * @param recentDays 最近天数
     * @return 返回统计数据
     */
    public RecentVisitDataVO getRecentVisitData(int recentDays) {
        if (recentDays <= 0) {
            recentDays = 7;
        }
        List<String> dateList = DateUtil.getDateList(recentDays);

        List<DayPvUvVO> dayPvUvDTOList = sysEventLogDao.getRecentVisitData(recentDays);
        List<Integer> data1 = Lists.newArrayList();
        List<Integer> data2 = Lists.newArrayList();
        if (CollectionUtils.isEmpty(dayPvUvDTOList)) {
            for (int i = 0; i < recentDays; i++) {
                data1.add(0);
                data2.add(0);
            }
        } else {
            Map<String, DayPvUvVO> map = dayPvUvDTOList.stream().collect(Collectors.toMap(DayPvUvVO::getDate, x -> x));
            dateList.forEach(date -> {
                if (map.containsKey(date)) {
                    data1.add(map.get(date).getPv());
                    data2.add(map.get(date).getUv());
                } else {
                    data1.add(0);
                    data2.add(0);
                }
            });
        }
        List<VisitItemVO> datasets = Lists.newArrayList();
        datasets.add(VisitItemVO.builder().label("访问次数").data(data1).build());
        datasets.add(VisitItemVO.builder().label("访问用户数").data(data2).build());

        // 返回
        return RecentVisitDataVO.builder().dateList(dateList).datasets(datasets).build();
    }

}
